import sys
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5 import QtWidgets
from PyQt5.QtCore import QStringListModel
import os
from TestUI import Ui_Form
from generate_form import morphology_analysis

class MainWidgets(QWidget,Ui_Form):

    mor_object = None

    def __init__(self):
        super(MainWidgets, self).__init__()
        self.setupUi(self)
        self.OpenText.clicked.connect(self.ClickToOpenDialog)
        self.DealWithText.clicked.connect(self.ClickToDealWithText)
        self.mor_object = morphology_analysis()


    def ClickToOpenDialog(self):
        self.mor_object.strPath, fileType = QtWidgets.QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(),"All Files(*);;Text Files(*.txt)")
        with open(self.mor_object.strPath,mode='r',encoding='utf-8') as file:
            self.mor_object.sts = file.read()
            self.textEdit.setText(self.mor_object.sts)


    def ClickToDealWithText(self):
        self.mor_object.sts = self.textEdit.toPlainText()
        self.mor_object.generate_form_of_group(self.mor_object.readtext())
        model = QStringListModel()
        model.setStringList(self.mor_object.analysis_form)
        self.ResultOfAnalysis.setModel(model)
        self.mor_object.start_up()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWidgets()
    w.show()
    sys.exit(app.exec_())